package com.example.system.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.starter.util.Response;
import com.example.starter.webmvc.valid.CreateValid;
import com.example.starter.webmvc.valid.UpdateValid;
import com.example.system.constant.PM;
import com.example.system.convert.TenantConvert;
import com.example.system.dto.TenantDto;
import com.example.system.dto.TenantParam;
import com.example.system.entity.Tenant;
import com.example.system.service.ITenantService;
import com.example.system.vo.TenantVo;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * @author 王令
 * @since 2022-10-21
 */
@RequestMapping("/tenant")
@RequiredArgsConstructor
@RestController
public class TenantController {

    private final ITenantService service;

    @PreAuthorize("hasAuthority('"+ PM.SYS_TENANT_WRITE +"')")
    @PostMapping
    public Response<Boolean> create(@RequestBody @Validated(CreateValid.class) TenantDto tenantDto) {
        service.create(tenantDto);
        return Response.ok();
    }

    @PreAuthorize("hasAuthority('"+ PM.SYS_TENANT_WRITE +"')")
    @PutMapping
    public Response<Boolean> update(@RequestBody @Validated(UpdateValid.class) TenantDto tenantDto) {
        service.update(tenantDto);
        return Response.ok();
    }

    @PreAuthorize("hasAuthority('"+ PM.GRANT_TENANT_AUTHORIZATION +"')")
    @PutMapping("/grant/{tenantId}/authority/")
    public Response<Boolean> grantAuthority(@PathVariable("tenantId") String tenantId, @RequestBody List<Long> authoritiesId) {
        service.grantAuthority(tenantId, authoritiesId);
        return Response.ok();
    }

    @PreAuthorize("hasAuthority('"+ PM.SYS_TENANT_WRITE +"')")
    @PutMapping("/{tenantId}/admin/{username}/setup")
    public Response<Boolean> setUpAdmin(@PathVariable("tenantId") String tenantId, @PathVariable("username") String username) {
        service.setUpAdmin(tenantId, username);
        return Response.ok();
    }

    @PreAuthorize("hasAuthority('"+ PM.SYS_TENANT_WRITE +"')")
    @DeleteMapping("/{id}")
    public Response<Boolean> deleteById(@PathVariable("id") Long id) {
        service.removeById(id);
        return Response.ok();
    }

    @PreAuthorize("hasAuthority('"+ PM.SYS_TENANT_READ +"')")
    @GetMapping("/{id}")
    public Response<TenantVo> findById(@PathVariable("id") Long id) {
        return Response.ok(TenantConvert.INSTANCE.toTenantVo(service.getById(id)));
    }

    @PreAuthorize("hasAuthority('"+ PM.SYS_TENANT_READ +"')")
    @GetMapping("/pagination")
    public Response<Page<TenantVo>> listWithPagination( Pageable pageable, TenantParam param) {
        return Response.ok(service.listWithPagination(pageable, param));
    }

    @GetMapping("/list")
    public Response<List<TenantVo>> listAll() {
        return Response.ok(TenantConvert.INSTANCE.toTenantVoList(service.list(Wrappers.<Tenant>lambdaQuery().eq(Tenant::getEnabled, true))));
    }

    @PreAuthorize("hasAuthority('"+ PM.SYS_TENANT_READ +"')")
    @GetMapping("/tenant/{id}")
    public Response<List<Long>> listAuthorityIdByTenantId(@PathVariable("id") String tenantId) {
        return Response.ok(service.listAuthorityIdByTenantId(tenantId));
    }

}
